#include "stdafx.h"
#include "ui_focusable_control.h"

_SSL_BEGIN
_SSUIL_BEGIN

UIFocusableControl::UIFocusableControl()
{
	//
	properties_ |= kControlPropertyHittest|kControlPropertyTabStop;
	show_border_ = false;
	background_transparent_ = true;

	//
	overed_ = false;
	over_sensitive_ = true;
	pressed_ = false;
	press_sensitive_ = true;
	focus_sensitive_ = true;

	background_overed_color_ = 0x999999;
	// background_overed_image_;
	background_pressed_color_ = 0xCCCCCC;
	// background_pressed_image_;
	background_focused_color_ = 0x444444;
	// background_focused_image_;
}

UIFocusableControl::~UIFocusableControl()
{

}

bool UIFocusableControl::ApplyAttributes(IUIBuilderInterface *ui_builder, 
	IUIXMLDocumentInterface *xml_document, UIXmlNode node)
{
	if(!__super::ApplyAttributes(ui_builder, xml_document, node))
		return false;

	UIXmlAttribute attribute = NULL;

	//
	attribute = xml_document->FirstAttribute(node, "over_sensitive");
	if(attribute != NULL)
	{
		bool b = false;
		if(UIValueParseUtil::ParseBool(xml_document->AttributeValue(attribute), b) == 1)
		{
			SetOverSensitive(b);
		}
	}

	attribute = xml_document->FirstAttribute(node, "press_sensitive");
	if(attribute != NULL)
	{
		bool b = false;
		if(UIValueParseUtil::ParseBool(xml_document->AttributeValue(attribute), b) == 1)
		{
			SetPressSensitive(b);
		}
	}

	attribute = xml_document->FirstAttribute(node, "focus_sensitive");
	if(attribute != NULL)
	{
		bool b = false;
		if(UIValueParseUtil::ParseBool(xml_document->AttributeValue(attribute), b) == 1)
		{
			SetFocusSensitive(b);
		}
	}

	attribute = xml_document->FirstAttribute(node, "overed_background_color");
	if(attribute != NULL)
	{
		unsigned long c = 0;
		if(UIValueParseUtil::ParseColor(xml_document->AttributeValue(attribute), c) == 1)
		{
			SetOveredBackgroundColor(c);
		}
	}

	attribute = xml_document->FirstAttribute(node, "pressed_background_color");
	if(attribute != NULL)
	{
		unsigned long c = 0;
		if(UIValueParseUtil::ParseColor(xml_document->AttributeValue(attribute), c) == 1)
		{
			SetPressedBackgroundColor(c);
		}
	}

	attribute = xml_document->FirstAttribute(node, "focused_background_color");
	if(attribute != NULL)
	{
		unsigned long c = 0;
		if(UIValueParseUtil::ParseColor(xml_document->AttributeValue(attribute), c) == 1)
		{
			SetFocusedBackgroundColor(c);
		}
	}

	attribute = xml_document->FirstAttribute(node, "overed_background_image");
	if(attribute != NULL)
	{
		UIPresetImage i;
		if(UIValueParseUtil::ParseImage(xml_document->AttributeValue(attribute), i) == 1)
		{
			SetOveredBackgroundImage(i);
		}
	}


	attribute = xml_document->FirstAttribute(node, "pressed_background_image");
	if(attribute != NULL)
	{
		UIPresetImage i;
		if(UIValueParseUtil::ParseImage(xml_document->AttributeValue(attribute), i) == 1)
		{
			SetPressedBackgroundImage(i);
		}
	}

	attribute = xml_document->FirstAttribute(node, "focused_background_image");
	if(attribute != NULL)
	{
		UIPresetImage i;
		if(UIValueParseUtil::ParseImage(xml_document->AttributeValue(attribute), i) == 1)
		{
			SetFocusedBackgroundImage(i);
		}
	}

	attribute = xml_document->FirstAttribute(node, "focused_overed_background_image");
	if(attribute != NULL)
	{
		UIPresetImage i;
		if(UIValueParseUtil::ParseImage(xml_document->AttributeValue(attribute), i) == 1)
		{
			SetFocusedOveredBackgroundImage(i);
		}
	}

	attribute = xml_document->FirstAttribute(node, "focused_pressed_background_image");
	if(attribute != NULL)
	{
		UIPresetImage i;
		if(UIValueParseUtil::ParseImage(xml_document->AttributeValue(attribute), i) == 1)
		{
			SetFocusedPressedBackgroundImage(i);
		}
	}

	return true;
}

void UIFocusableControl::DoEvent(UIEvent &evt)
{
	if(evt.type == kEvtMouseEnter)
	{
		if(!over_sensitive_)
			return;
		overed_ = true;
		OnStateChangedCB();
		Invalidate();
		return;
	}

	if(evt.type == kEvtMouseLeave)
	{
		if(overed_)
		{
			overed_ = false;
			OnStateChangedCB();
			Invalidate();
		}
		return;
	}

	if(evt.type == kEvtLButtonDown || evt.type == kEvtRButtonDown)
	{
		if(!press_sensitive_)
			return;
		pressed_ = true;
		OnStateChangedCB();
		Invalidate();
		return;
	}

	if(evt.type == kEvtButtonUp)
	{
		if(pressed_)
		{
			if(this->compare(evt.relative_control))
			{
				if(::PtInRect(&GetVisiblePositionOnClient(), evt.mouse_point))
				{
					DoAction();
				}
			}

			pressed_ = false;
			OnStateChangedCB();
			Invalidate();
		}

		return;
	}

	__super::DoEvent(evt);
}

UINT UIFocusableControl::GetProperties()
{
	UINT properties = __super::GetProperties();
	if(SS_FLAG_ISSET(properties, kControlPropertyTabStop))
	{
		if(GetVisibleViewAreaInMyPosition().IsEmptry())
			properties &= ~kControlPropertyTabStop;
	}
	return properties;
}

void UIFocusableControl::DoAction()
{
	if(OnClick)
	{
		OnClick(static_cast<IUIControlInterface*>(this));
	}
	SendNotify(UINotify(_T("click"), this));
}

unsigned long UIFocusableControl::GetBackgroundColor()
{
	if(!IsEnabled())
		return __super::GetBackgroundColor();
	if(pressed_)
		return background_pressed_color_;
	if(overed_)
		return background_overed_color_;
	if(IsFocused())
		return background_focused_color_;
	return __super::GetBackgroundColor();
}

UIPresetImage& UIFocusableControl::GetBackgroundImage()
{
	if(!IsEnabled())
		return __super::GetBackgroundImage();

	if(IsFocused())
	{
		if(pressed_)
		{
			if(!background_focused_pressed_image_.image_name.empty())
				return background_focused_pressed_image_;
		}else if(overed_)
		{
			if(!background_focused_overed_image_.image_name.empty())
				return background_focused_overed_image_;
		}else if(!background_focused_image_.image_name.empty())
		{
			return background_focused_image_;
		}
	}
	
	if(pressed_ && !background_pressed_image_.image_name.empty())
		return background_pressed_image_;

	if(overed_ && !background_overed_image_.image_name.empty())
		return background_overed_image_;

	return __super::GetBackgroundImage();
}

bool UIFocusableControl::IsOverSensitive()
{
	return over_sensitive_;
}

void UIFocusableControl::SetOverSensitive(bool flag)
{
	over_sensitive_ = flag;
}

bool UIFocusableControl::IsPressSensitive()
{
	return press_sensitive_;
}

void UIFocusableControl::SetPressSensitive(bool flag)
{
	press_sensitive_ = flag;
}

bool UIFocusableControl::IsFocusSensitive()
{
	return focus_sensitive_;
}

void UIFocusableControl::SetFocusSensitive(bool flag)
{
	focus_sensitive_ = flag;
}

bool UIFocusableControl::SetFocus(bool f)
{
	if(!IsFocusSensitive())
		return false;
	scoped_ref_ptr<IUIManagerInterface> win_man = GetWindowManager();
	if(win_man == NULL)
		return false;

	if(f)
	{
		return win_man->SetFocus(this);
	}

	return win_man->KillFocus(this);
}

bool UIFocusableControl::IsFocused()
{
	scoped_ref_ptr<IUIManagerInterface> win_man = GetWindowManager();
	if(win_man == NULL)
		return false;
	scoped_ref_ptr<IUIControlInterface> focus_control = win_man->GetFocusControl();
	if(focus_control == NULL)
		return false;
	return focus_control->compare(this);
}

void UIFocusableControl::SetOveredBackgroundColor(unsigned long color)
{
	if(background_overed_color_ == color)
		return;
	background_overed_color_ = color;
	Invalidate();
}

void UIFocusableControl::SetOveredBackgroundImage(const UIPresetImage &img)
{
	if(background_overed_image_.Compare(img))
		return;
	background_overed_image_ = img;
	Invalidate();
}

void UIFocusableControl::SetPressedBackgroundColor(unsigned long color)
{
	if(background_pressed_color_ == color)
		return;
	background_pressed_color_ = color;
	Invalidate();
}

void UIFocusableControl::SetPressedBackgroundImage(const UIPresetImage &img)
{
	if(background_pressed_image_.Compare(img))
		return;
	background_pressed_image_ = img;
	Invalidate();
}

void UIFocusableControl::SetFocusedBackgroundColor(unsigned long color)
{
	if(background_focused_color_ == color)
		return;
	background_focused_color_ = color;
	Invalidate();
}

void UIFocusableControl::SetFocusedBackgroundImage(const UIPresetImage &img)
{
	if(background_focused_image_.Compare(img))
		return;
	background_focused_image_ = img;
	Invalidate();
}

void UIFocusableControl::SetFocusedOveredBackgroundImage(const UIPresetImage &img)
{
	if(background_focused_overed_image_.Compare(img))
		return;
	background_focused_overed_image_ = img;
	Invalidate();
}

void UIFocusableControl::SetFocusedPressedBackgroundImage(const UIPresetImage &img)
{
	if(background_focused_pressed_image_.Compare(img))
		return;
	background_focused_pressed_image_ = img;
	Invalidate();
}

_SSUIL_END
_SSL_END
